//
//  Created by Rene Dohan on 11/22/11.
//


#import "TransactionsController.h"
#import "TransactionData.h"
#import "MBProgressHUD.h"
#import "LoadRowRequest.h"

static NSInteger const STARTUP_COUNT = 100;

@implementation TransactionsController {
@private
    TransactionData *data;
    NSString *imageApproved;
    NSString *imageDeclained;
    NSString *imagePending;
    NSString *imageRefund;
    NSInteger dataCount;
}

@synthesize tableView;

- (void)viewDidLoad {
    imageApproved = [[NSBundle mainBundle] pathForResource:@"ic_transaction_approved" ofType:@"png"];
    imageDeclained = [[NSBundle mainBundle] pathForResource:@"ic_transaction_declined" ofType:@"png"];
    imagePending = [[NSBundle mainBundle] pathForResource:@"ic_transaction_pending" ofType:@"png"];
    imageRefund = [[NSBundle mainBundle] pathForResource:@"ic_transaction_refund" ofType:@"png"];

    dataCount = STARTUP_COUNT;

    data = [TransactionData new];
    data.listener = self;
    [data reset];
    [super viewDidLoad];
}

- (IBAction)onReloadClick:(UIButton *)sender {
    [data reset];
    [tableView reloadData];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return dataCount;
}

- (void)resetCell:(UITableViewCell *)cell {
}

- (UITableViewCell *)tableView:(UITableView *)uiTableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [uiTableView dequeueReusableCellWithIdentifier:@"MerchantTableCell"];
    [MBProgressHUD hideHUDForView:cell animated:false];
    [self resetCell:cell];
    [data loadRow:indexPath.row :cell];
    return cell;
}

- (UIImage *)createStatusImage:(NSString *)imageId {
    if ([imageId isEqualToString:@"A"]) return [UIImage imageWithContentsOfFile:imageApproved];
    else if ([imageId isEqualToString:@"D"]) return [UIImage imageWithContentsOfFile:imageDeclained];
    else if ([imageId isEqualToString:@"P"]) return [UIImage imageWithContentsOfFile:imagePending];
    else if ([imageId isEqualToString:@"R"])return [UIImage imageWithContentsOfFile:imageRefund];
    @throw [NSException exceptionWithName:@"Unrecognized Status" reason:nil userInfo:nil];
}

- (void)onRowDataReady:(NSDictionary *)rowData :(LoadRowRequest *)request {
}

- (void)onDataCount:(NSInteger)count {
    BOOL firstLoad = dataCount == STARTUP_COUNT;
    dataCount = count;
    if (firstLoad) [tableView reloadData];
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    return YES;
}
@end